<?php
namespace Admin\Controller;

use Common\Controller\AdminbaseController;
class GroupController extends AdminbaseController{
	

	
	function _initialize() {
		parent::_initialize();
		$this->db = D("Group");
	}
	
	function index(){
		
	
		
	
		$count=$this->db->count();
		$page = $this->page($count);
		$list=$this->db->limit($page->firstRow . ',' . $page->listRows)->select();
		
		$this->assign("Page",$page->show(""));
		
		$this->assign("list",$list);
		
		
		$this->display();
		
		
		
		
	}
	

	
	function add(){
		
		if(IS_POST){
			
		
			if ($this->db->create()) {
				if ($this->db->add()!==false) {
					$this->success("添加成功！");
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		}else{
		
		$this->display("form");
		}
		}
	
	
	function edit(){
		
		
		if(IS_POST){
			if ($this->db->create()) {
				
				if ($this->db->save()!==false) {
					$this->success("保存成功！", U("index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->db->getError());
			}
		
		}else{
	
		$id= intval(I("get.id"));
		if(!$id){
			$this->error("缺少参数！");
		}
	
		
		$r=$this->db->where("id=$id")->find();
		$this->assign("edit",true);
		$this->assign("data",$r);
		$this->display("form");
		}
	}
	

	
	

	function delete(){
		
			$id = intval(I("get.id"));
	
			
			if ($this->db->delete($id)) {
				
				$this->success("删除成功！");
			} else {
				$this->error("删除失败！");
			}


	}
	

	

	
	//排序
	public function listorders() {
		$status = parent::_listorders($this->db);
		if ($status) {
			$this->success("排序更新成功！");
		} else {
			$this->error("排序更新失败！");
		}
	}
	/**
	 * 改变默认值
	 */
	public function changedefault() {
		$id = $_REQUEST[id];
		
		
		
		$r = $this->db->where(array("id" => $id))->find();
		
		$status = $r['default'] ? 0 : 1;
			
		
	
		if ($r['country_id']) {
			
		
			$result1=$this->db->query("update `__PREFIX__city` SET `default`=0 WHERE `country_id` = '$r[country_id]'");
			$result=$this->db->save(array("default"=>$status,"id"=>$id));
	
			if ($result1 !== false&&$result !== false) {
	
				$this->success("转换成功！");
	
			} else {
			$this->error("转换失败！");
			}
		} else {
			$this->error("不存在！");
		}
	}
}